/**
 * Fuzzy Refrigerator: Refrigerator with Fuzzy Logic as temperature controller
 * Copyright (C) 2012  Izwan Azmi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

 /**
  * @file refrigerator.cpp
  * @brief
  * @author Izwan Azmi <izwanazmi90@gmail.com>
  */

#include "refrigerator.h"

#include <fstream>
#include <iostream>

using namespace std;

refrigerator::refrigerator() {
    maxNumVarOut[0] = 3;

    // Set trapezoid bagi suhu
    ref.suhu.sejuk = init_trapz(-5, 2, 0, 0, kiri);
    ref.suhu.ideal = init_trapz(-5, 2, 2, 5, biasa);
    ref.suhu.panas = init_trapz(2, 5, 0, 0, kanan);

    // Set trapezoid bagi rate
    ref.rate.perlahan = init_trapz(-2, 0, 0, 2, biasa);
    ref.rate.lajuN = init_trapz(-2, 0, 0, 0, kiri);
    ref.rate.lajuP = init_trapz(0, 2, 0, 0, kanan);

    // Set trapezoid bagi power
    ref.pow.lemah = init_trapz(0, 1, 0, 0, kiri);
    ref.pow.sederhana = init_trapz(0, 1, 1, 2, biasa);
    ref.pow.kuat = init_trapz(1, 2, 0, 0, kanan);
}

refrigerator::~refrigerator() {
    //dtor
}

float refrigerator::start(float temp, float rate) {
    // Fuzzification of the input variables
    sejuk = fuzzification(temp, ref.suhu.sejuk);
    ideal = fuzzification(temp, ref.suhu.ideal);
    panas = fuzzification(temp, ref.suhu.panas);
    perlahan = fuzzification(rate, ref.rate.perlahan);
    if(rate < 0)
        return laju = fuzzification(rate, ref.rate.lajuN);
    else
        return laju = fuzzification(rate, ref.rate.lajuP);

    // Rule evaluation
    // Peraturan 1 : JIKA Suhu adalah sejuk DAN Rate adalah Perlahan MAKA Power adalah Kuat
    input[0] = sejuk;
    input[1] = perlahan;
    kuat[0] = getMin(input, 2);
    // Peraturan 2 : JIKA Suhu adalah sejuk DAN Rate adalah Laju MAKA Power adalah Sederhana
    input[0] = sejuk;
    input[1] = laju;
    sederhana[0] = getMin(input, 2);
    // Peraturan 3 : JIKA Suhu adalah Ideal DAN Rate adalah Perlahan MAKA Power adalah Sederhana
    input[0] = ideal;
    input[1] = perlahan;
    sederhana[1] = getMin(input, 2);
    // Peraturan 4 : JIKA Suhu adalah Ideal DAN Rate adalah Laju MAKA Power adalah Lemah
    input[0] = ideal;
    input[1] = laju;
    lemah = getMin(input, 2);
    // Peraturan 5 : JIKA Suhu adalah Panas DAN Rate adalah Perlahan MAKA Power adalah Kuat
    input[0] = panas;
    input[1] = perlahan;
    kuat[1] = getMin(input, 2);
    // Peraturan 6 : JIKA Suhu adalah Panas DAN Rate adalah Laju MAKA Power adalah Sederhana
    input[0] = panas;
    input[1] = laju;
    sederhana[2] = getMin(input, 2);

    // Aggregation of the rule outputs
    trapezoid varOut[3] = {ref.pow.lemah, ref.pow.sederhana, ref.pow.kuat};
    float output[3] = {lemah, getMax(sederhana, 3), getMax(kuat, 2)};
    clipped(output, varOut, maxNumVarOut[0]);

    // Defuzzification
    return defuzzification(varOut, output, 0, 2, 0.001, maxNumVarOut[0]);

    return 10101;
}

void refrigerator::report(int kes)
{
    ofstream ofs;

    // Suhu
    ofs.open("suhu.csv");
    for(float i = -5; i < 5; i += 0.001)
    {
        ofs << i << " ";
        ofs << fuzzification(i, ref.suhu.sejuk) << " ";
        ofs << fuzzification(i, ref.suhu.ideal) << " ";
        ofs << fuzzification(i, ref.suhu.panas) << endl;
    }
    ofs.close();

    // Rate
    ofs.open("rate.csv");
    for(float i = -2; i < 2; i += 0.001)
    {
        ofs << i << " ";
        ofs << fuzzification(i, ref.rate.perlahan) << " ";
        if(i < 0)
            ofs << fuzzification(i, ref.rate.lajuN) << endl;
        else
            ofs << fuzzification(i, ref.rate.lajuP) << endl;
    }
    ofs.close();

    // Power
    ofs.open("power.csv");
    for(float i = 0; i < 2; i += 0.001)
    {
        ofs << i << " ";
        ofs << fuzzification(i, ref.pow.lemah) << " ";
        ofs << fuzzification(i, ref.pow.sederhana) << " ";
        ofs << fuzzification(i, ref.pow.kuat) << endl;
    }
    ofs.close();

    // output
    trapezoid trz[3] = {ref.pow.lemah, ref.pow.sederhana, ref.pow.kuat};
    float out[3] = {lemah, getMax(sederhana, 3), getMax(kuat, 2)};

    char name[] = "kes*.csv";
    name[3] = kes + '0';

    ofs.open(name);
    for(int i = 0; i < 3; i++) {
        for(float j = cl[i].lim_kiri; j <= cl[i].lim_kanan; j += 0.01) {
            cout << j << " ";
            cout << cl[i].lim_kiri << " ";
            if(fuzzification(j, trz[i]) > out[i]) {
                cout << out[i] << endl;
            } else {
                cout << fuzzification(j, trz[i]) << endl;
            }
        }
    }
    ofs.close();
}
